package com.hzh.encryptiondemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    private String path = "http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";

    //毛泽东 == %E6%AF%9B%E6%B3%BD%E4%B8%9C
    private String url1 = "https://www.baidu.com/s?wd=%E6%AF%9B%E6%B3%BD%E4%B8%9C&rsv_spt=1&rsv_iqid=0xf4857b1900001907&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=10&rsv_sug1=4&rsv_sug7=100&rsv_sug2=0&inputT=5441&rsv_sug4=5441&rsv_sug=1";
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 101:
                    byte[] ret = (byte[]) msg.obj;
                    Log.d("flag", "---------->handleMessage: " +new String(ret));
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getInternetByte();
    }

    public void getInternetByte() {

        //URLEncode编码
        try {
            String encode = URLEncoder.encode("毛泽东","UTF-8");

            Log.d("flag", "---------->getInternetByte: 进行URLEncode之后的内容：" +encode);

            //使用URLDecoder进行解码
            //https://www.baidu.com/s?wd=
            // %E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89
            // &rsv_spt=1&rsv_iqid=0xf4857b1900001907&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&inputT=11607&rsv_t=363feGcXiJc4qv0CEHZtL4CNRDmPyu4gjeeZ%2FLgdO6GZtAw8NM8OqVO18PPQT6dU%2Fawu&rsv_sug3=31&rsv_sug1=23&rsv_sug7=100&oq=%E6%AF%9B%E6%B3%BD%E4%B8%9C&rsv_pq=eb7ad5bb00001822&rsv_sug2=0&rsv_sug4=12886
            String str = "%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89";

            String decode = URLDecoder.decode(str,"UTF-8");

            Log.d("flag", "---------->getInternetByte: 进行URLDecoder之后的内容是：" +decode);

            path = path+encode+"s";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                InputStream is = null;
//                ByteArrayOutputStream baos = null;
//                try {
//                    URL url = new URL(url1);
//                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//                    conn.setConnectTimeout(5000);
//                    if (conn.getResponseCode()==200){
//                        is = conn.getInputStream();
//                        baos = new ByteArrayOutputStream();
//                        byte[] buf = new byte[1024*8];
//                        int len = 0;
//                        while((len=is.read(buf))!=-1){
//                            baos.write(buf,0,len);
//                        }
//                        mHandler.sendMessage(Message.obtain(mHandler,101,baos.toByteArray()));
//                    }
//                } catch (MalformedURLException e) {
//                    Log.d("flag", "---------->run: MalformedURLException");
//                    e.printStackTrace();
//                } catch (IOException e) {
//                    Log.d("flag", "---------->run: IOException");
//                    e.printStackTrace();
//                }finally {
//                    Log.d("flag", "---------->run: finally");
//                    if (baos != null) {
//                        try {
//                            baos.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    if (is != null) {
//                        try {
//                            is.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//
//
//            }
//        }).start();
    }
}
